package com.avamanager.app.controller;

import com.avamanager.app.domain.AppStudent;
import com.avamanager.app.service.IAppStudentService;
import com.avamanager.common.annotation.Log;
import com.avamanager.common.core.controller.BaseController;
import com.avamanager.common.core.domain.AjaxResult;
import com.avamanager.common.core.domain.entity.SysRole;
import com.avamanager.common.core.domain.entity.SysUser;
import com.avamanager.common.core.page.TableDataInfo;
import com.avamanager.common.enums.BusinessType;
import com.avamanager.common.utils.SecurityUtils;
import com.avamanager.common.utils.StringUtils;
import com.avamanager.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.avamanager.system.service.ISysUserService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生信息Controller
 * 
 * @author FenvyYe
 * @date 2022-08-14
 */
@RestController
@RequestMapping("/app/student")
public class AppStudentController extends BaseController
{
    @Autowired
    private IAppStudentService appStudentService;
//    @Autowired
//    private ISysUserService userService;


    /**
     * 查询学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('app:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppStudent appStudent)
    {
        startPage();
        List<AppStudent> list = appStudentService.selectAppStudentList(appStudent);
        return getDataTable(list);
    }

//    @PreAuthorize("@ss.hasPermi('app:student:import')")
//    @Log(title = "学生管理", businessType = BusinessType.IMPORT)
//    @PostMapping("/importData")
//    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {



        // 测试结果是文件是正确且有内容的
//        String path = "D:/JiXie/";
//        String fileName = file.getOriginalFilename();
//        String substring = fileName.substring(fileName.lastIndexOf("."));
//        File uploadFile = new File(path + System.currentTimeMillis() + substring);
//        file.transferTo(uploadFile);

//        ExcelUtil<AppStudent> util = new ExcelUtil<AppStudent>(AppStudent.class);
//        List<AppStudent> studentList = util.importExcel(file.getInputStream());
//        String message = appStudentService.importStudent(studentList, updateSupport, SecurityUtils.getUsername());
//        return AjaxResult.success(message);
//    }
//
//    @GetMapping("/importTemplate")
//    public AjaxResult importTemplate() {
//        ExcelUtil<AppStudent> util = new ExcelUtil<>(AppStudent.class);
//        return util.importTemplateExcel("学生数据模板");
//    }
//
    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('app:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppStudent appStudent)
    {
        List<AppStudent> list = appStudentService.selectAppStudentList(appStudent);
        ExcelUtil<AppStudent> util = new ExcelUtil<AppStudent>(AppStudent.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return AjaxResult.success(appStudentService.selectAppStudentByStudentId(studentId));

//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("students", appStudentService.selectAppStudentList());
//        if (StringUtils.isNotNull(studentId))
//        {
//            SysUser sysUser = userService.selectUserById(studentId);
//            ajax.put(AjaxResult.DATA_TAG, sysUser);
//            ajax.put("userIds", postService.selectPostListByUserId(studentId));
//        }
//        return ajax;
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('app:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppStudent appStudent)
    {
        return toAjax(appStudentService.insertAppStudent(appStudent));
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('app:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppStudent appStudent)
    {
        return toAjax(appStudentService.updateAppStudent(appStudent));
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('app:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(appStudentService.deleteAppStudentByStudentIds(studentIds));
    }
}
